package com.iafme.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iafme.dto.req.ProjectReqDTO;
import com.iafme.dto.res.ProjectResDTO;
import com.iafme.model.Project;
import com.iafme.service.IProjectService;
import com.systemframework.annotation.OperateAnnotation;
import com.systemframework.common.BaseCommonInterface;
import com.systemframework.enums.BusinessType;
import com.systemframework.util.Result;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author summer
 * @version 1.0
 * @date 2025/1/7 17:58
 */
@RestController
@RequestMapping(value = "/project")
@AllArgsConstructor
public class ProjectController implements BaseCommonInterface<ProjectReqDTO, ProjectResDTO> {

    private final IProjectService projectService;


    @OperateAnnotation(businessType = BusinessType.INSERT,name = "报考项目")
    @SaCheckPermission("enter:project:add")
    @Override
    public Result<String> save(@Valid ProjectReqDTO projectReqDTO) {
        Project convert = Convert.convert(Project.class, projectReqDTO);
        projectService.save(convert);
        return Result.successResult();
    }

    @OperateAnnotation(businessType = BusinessType.UPDATE,name = "报考项目")
    @SaCheckPermission("enter:project:edit")
    @Override
    public Result<String> update(@Valid ProjectReqDTO projectReqDTO) {
        Project convert = Convert.convert(Project.class, projectReqDTO);
        projectService.updateById(convert);
        return Result.successResult();
    }

    @OperateAnnotation(businessType = BusinessType.DELETE,name = "报考项目")
    @SaCheckPermission("enter:project:delete")
    @Override
    public Result<String> del(Integer id) {
        projectService.removeById(id);
        return Result.successResult();
    }

    @Override
    public Result<Page<ProjectResDTO>> lists(ProjectReqDTO projectReqDTO) {
        Page<ProjectResDTO> page=projectService.lists(projectReqDTO);
        return Result.successResult(page);
    }

    @Override
    public Result<ProjectResDTO> findById(Integer id) {
        Project project = projectService.getById(id);
        return Result.successResult(Convert.convert(ProjectResDTO.class, project));
    }

    @OperateAnnotation(businessType = BusinessType.BATCHDELETE,name = "报考项目")
    @SaCheckPermission("enter:project:delete")
    @Override
    public Result<String> batchDel(List<ProjectResDTO> list) {
        List<Integer> ids = list.stream().map(ProjectResDTO::getId).collect(Collectors.toList());
        projectService.removeByIds(ids);
        return Result.successResult();
    }

    @GetMapping(value = "/findAll")
    public List<ProjectResDTO> findAll(){
        List<Project> projects = projectService.list();
        return Convert.toList(ProjectResDTO.class, projects);
    }
}
